package com.bosssoft.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.baseParam.BaseParam;
import com.bosssoft.entity.dto.FibSealDTO;
import com.bosssoft.entity.dto.FibVoucherDTO;
import com.bosssoft.entity.po.FibSealPO;
import com.bosssoft.entity.po.FibVoucherPO;
import com.bosssoft.entity.vo.FibSealVO;
import com.bosssoft.entity.vo.FibVoucherVO;
import com.bosssoft.enums.EleSerCode;
import com.bosssoft.exception.DaoException;
import com.bosssoft.page.PageResult;
import com.bosssoft.response.Result;
import com.bosssoft.service.FibVoucherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Aria
 */
@Api(tags = "凭证表")
@RequestMapping("/voucher")
@RestController
public class FibVoucherController {

    @Autowired
    private FibVoucherService voucherService;

    @ApiOperation(value = "分页查询")
    @PostMapping("/getSealPage")
    public Result getSealPage(@RequestParam(defaultValue = "1") Integer pageNo,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            Page<FibVoucherPO> page = new Page<>(pageNo, pageSize);
            Page<FibVoucherPO> sealPage = voucherService.page(page);
            PageResult<FibVoucherPO> fibVoucherPOPageResult = new PageResult<>(page, sealPage.getRecords());
            return Result.ok().data("page", fibVoucherPOPageResult);
        } catch (DaoException e) {
            e.printStackTrace();
            return Result.error();
        }
    }


    @ApiOperation(value = "根据id更新,不能更新编码和模板")
    @PostMapping("/sealInsertOrUpdate")
    public Result sealInsertOrUpdate(@RequestBody @Validated({BaseParam.add.class}) FibVoucherVO FibVoucherVO) {
        FibVoucherDTO fibVoucherDTO = new FibVoucherDTO();
        BeanUtils.copyProperties(FibVoucherVO, fibVoucherDTO);
        try {
            voucherService.update(fibVoucherDTO);
        } catch (DaoException e) {
            if (e.getMessage() != null) {
                return Result.error(e.getCode(), e.getMessage());
            }
            return Result.error();
        }
        return Result.ok();
    }

    @ApiOperation(value = "根据id删除")
    @PostMapping("/sealDelete")
    public Result sealDelete(@RequestBody @Validated({BaseParam.delete.class}) FibVoucherVO fibVoucherVO) {
        FibVoucherDTO fibVoucherDTO = new FibVoucherDTO();
        BeanUtils.copyProperties(fibVoucherVO, fibVoucherDTO);
        try {
            voucherService.updateDeleted(fibVoucherDTO);
        } catch (DaoException e) {
            if (e.getMessage() != null) {
                return Result.error(e.getCode(), e.getMessage());
            }
            return Result.error();
        }
        return Result.ok();
    }

    @ApiOperation(value = "批量删除")
    @PostMapping("/sealDeleteList")
    public Result sealDeleteList(@RequestBody List<FibVoucherVO> fibVoucherVOList) {
        FibVoucherDTO fibVoucherDTO;
        try {
            for (FibVoucherVO fibVoucherVO : fibVoucherVOList) {
                fibVoucherDTO = new FibVoucherDTO();
                BeanUtils.copyProperties(fibVoucherVO, fibVoucherDTO);
                voucherService.updateDeleted(fibVoucherDTO);
            }
        } catch (DaoException e) {
            if (e.getMessage() != null) {
                return Result.error(e.getCode(), e.getMessage());
            }
            return Result.error();
        }
        return Result.ok();
    }

    @ApiOperation(value = "查询所有数据")
    @PostMapping("/getSealAll")
    public Result getSealAll() {
        try {
            List<FibVoucherPO> list = voucherService.list();
            return Result.ok().data("list", list);
        } catch (DaoException e) {
            e.printStackTrace();
            return Result.error();
        }
    }
}
